package com.example.module_word.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.bdy.translate.util.BdyPhotoTranslateTool
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.module_word.databinding.ActivityTranslatePictureBinding
import com.example.module_word.databinding.ItemTranslateBinding
import com.fenghuajueli.libbasecoreui.recycler.bindAdapter
import com.ll.module_translate.entity.TransResult
import com.ll.module_translate.utils.Scanning

/**
 * @Author: ll
 * @CreateTime: 2021/10/30 16:13
 */
class TranslatePictureActivity : AppCompatActivity() {


    private lateinit var translateTool: BdyPhotoTranslateTool

    private lateinit var result:MutableList<TransResult>
    companion object {
        const val TAG = "TranslatePictureActivit"
        const val PICURI = "picUri"
        const val ORITEXT = "ORITEXT"
        const val TRANTEXT = "TRANTEXT"
        fun start(
            context: Context,
            result: MutableList<TransResult>,
            picUri: Uri?,
            oriText: CharSequence,
            tranText: CharSequence
        ) {
            context.startActivity(Intent(context, TranslatePictureActivity::class.java)
                .putExtra(TAG, result as java.io.Serializable)
                .putExtra(PICURI, picUri)
                .putExtra(ORITEXT, oriText)
                .putExtra(TRANTEXT, tranText)
            )
        }
    }

    private val context: Context = this
    private val binding by lazy { ActivityTranslatePictureBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        result = intent.getSerializableExtra(TAG) as MutableList<TransResult>

        val mCurUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent?.getParcelableExtra(PICURI, Uri::class.java)
        } else {
            intent?.getParcelableExtra<Uri>(PICURI)
        }

        val textOri = intent.getStringExtra(ORITEXT)
        val textTran = intent.getStringExtra(TRANTEXT)

        Glide.with(this).load(mCurUri).into(binding.ivPicture)

        with(binding) {
            back.setOnClickListener { finish() }
        }
        LogUtils.d(Scanning.ScanFromToEn().first, Scanning.ScanFromToEn().second)

        binding.rvTrans.bindAdapter<TransResult, ItemTranslateBinding> (result){
            layoutManger = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

            onBindView2 { holder, view, data ->
                view.tvOr.text = textOri
                view.tvTran.text = textTran
                view.tvWord.text = data.src
                view.tvTranslate.text = data.dst
                view.ivCopyOri.setOnClickListener{ copyText(context,data.src) }
                view.ivCopyTran.setOnClickListener{ copyText(context,data.dst) }
            }


        }
        binding.rvTrans.itemAnimator = null
    }
    private fun copyText(context: Context, content: String) {
        if (content.isEmpty()) {
            ToastUtils.showLong("无内容可复制")
            return
        }
        (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
            .setPrimaryClip(ClipData.newPlainText("Label", content))
        ToastUtils.showLong("已复制")
    }
}